//操作mysql数据库
const { conn ,execcmd} = require('../conn/db')
async function checkuser(username, pwd) {

    
    const sql1 = 'select * from users where username=? and pwd=?';

    const mypromise = new Promise((resolve, reject) => {


        conn.query(sql1, [username, pwd], (err, result) => {
            if (err) {
                reject(err)
            }
            else {
                resolve(result)

            }
        })


    });

    const result = await mypromise
    console.log(result.length)
    if (result.length)
        return result[0]
    else return false

    //存到缓存，后面从缓存里面获取
}



async function add(userInfo) {
    const addSql = "INSERT INTO users(username,pwd)VALUES(?,?)";

    const result = await execcmd(addSql, [userInfo.username, userInfo.pwd])

    return result
    // if (result && result.affectedRows > 0) {
    //     return await  getSingle(result.insertId)
    // }
    // else return null
    

}

module.exports = { checkuser,add }